package com.atguigu.tms.mock.service.impl;

import com.atguigu.tms.mock.bean.*;
import com.atguigu.tms.mock.constant.TmsConstant;
import com.atguigu.tms.mock.mapper.ExpressTaskDeliveryMapper;
import com.atguigu.tms.mock.service.BaseOrganService;
import com.atguigu.tms.mock.service.ExpressTaskDeliveryService;
import com.atguigu.tms.mock.service.OrderTraceLogService;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.atguigu.tms.mock.constant.TmsConstant.TASK_TYPE_COLLECT;

/**
 * <p>
 * 取件、派件任务信息表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-06-27
 */
@Service
public class ExpressTaskDeliveryServiceImpl extends AdvServiceImpl<ExpressTaskDeliveryMapper, ExpressTaskDelivery> implements ExpressTaskDeliveryService {

    @Autowired
    BaseOrganService baseOrganService;

    @Autowired
    OrderTraceLogService orderTraceLogService;


    public  void  initExpressTaskDelivery(Date curDateTime, List<OrderOrgBound> orderOrgBoundList){

        List<ExpressTaskDelivery> expressTaskDeliveryList=new ArrayList<>();
        for (OrderOrgBound orderOrgBound : orderOrgBoundList) {

             genExpressTaskDelivery(curDateTime, orderOrgBound);

        }


    }

    public  ExpressTaskDelivery  genExpressTaskDelivery(Date curDateTime,  OrderOrgBound  orderOrgBound ){
        ExpressTaskDelivery expressTaskDelivery = new ExpressTaskDelivery();
        expressTaskDelivery.setStatus(TmsConstant.DELIVERY_STATUS_TODO);
        expressTaskDelivery.setCreateTime(curDateTime);
        expressTaskDelivery.setOrderId(orderOrgBound.getOrderId());
        expressTaskDelivery.setOrgId(orderOrgBound.getOrgId());
        BaseOrgan baseOrgan = baseOrganService.getById(orderOrgBound.getOrgId(),true);
        expressTaskDelivery.setOrgName(baseOrgan.getOrgName());
        expressTaskDelivery.setIsDeleted("0");

        saveOrUpdate (expressTaskDelivery   ,true);

        genOrderTraceLogDelivery(    expressTaskDelivery);
        return expressTaskDelivery;
    }


    //估计发送日期时间
    private Date estimateHandleTime(Date orderdDate,Integer handleTime){

        Date collectDate = DateUtils.addHours(orderdDate, handleTime);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(collectDate);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        if(hour>=19 || hour<=9){  //非工作时间顺延
            handleTime+=14;
        }
        Date estimateCollectTime = DateUtils.addHours(orderdDate, handleTime);
        return estimateCollectTime;

    }

    public void genOrderTraceLogDelivery(  ExpressTaskDelivery expressTaskDelivery){
        OrderTraceLog orderTraceLog = new OrderTraceLog();
        orderTraceLog.setOrderId(expressTaskDelivery.getOrderId() );
        orderTraceLog.setTraceDesc("快递已达目的地附近，准备派件");
        orderTraceLog.setCreateTime(expressTaskDelivery.getCreateTime());
        orderTraceLog.setCurTaskId(expressTaskDelivery.getId());
        orderTraceLog.setTaskType(TmsConstant.TASK_TYPE_DELIVERY);
        orderTraceLog.setIsDeleted("0");
        orderTraceLogService.saveOrUpdate(orderTraceLog );
    }

}
